package com.gift.service.Impl;


import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.BankCardMapper;
import com.gift.dao.SeTableMapper;
import com.gift.pojo.BankCard;
import com.gift.pojo.SeTable;
import com.gift.service.SpecialDealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("specialDealService")
@Transactional
public class SpecialDealServiceImpl implements SpecialDealService {

    @Resource
    SeTableMapper seTableMapper;
    @Resource
    BankCardMapper bankCardMapper;


    @Override
    public PaGing getAllBypr(int page, int rows, Map<String, Object> mapO) {
        int raa = (page-1) * rows;
        mapO.put("raa",raa);
        mapO.put("rows",rows);
        int total =seTableMapper.getCountAll(mapO);
        List<SeTable> seTableList= seTableMapper.getAllBypr(mapO);
        PaGing paGing= new PaGing(total,seTableList);
        return paGing;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServerResponse insetnewSeTable(SeTable newSeTable) {

        //银行卡余额
        BigDecimal sendMoney=newSeTable.getSpecialexpend();
        int bid=newSeTable.getBid();
        BankCard bankCard=bankCardMapper.selectByPrimaryKey(bid);
        bankCard.setInitialnum(bankCard.getInitialnum().subtract(sendMoney));
        int res1=bankCardMapper.updateByPrimaryKeySelective(bankCard);
        int res = seTableMapper.insert(newSeTable);
        if(res != 1 || res1 != 1){
            return ServerResponse.createByErrorMessage("新增特殊交易失败");
        }
        return ServerResponse.createBySuccessMessage("新增特殊交易成功");
    }

    @Override
    public SeTable getSeTableBySid(int sid) {

        return seTableMapper.selectByPrimaryKey(sid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServerResponse updataSeTable(SeTable updataSeTable) {

        //银行卡余额
        SeTable oldSeTable= seTableMapper.selectByPrimaryKey(updataSeTable.getSid());
        //加回
        BigDecimal sendMoney=oldSeTable.getSpecialexpend();
        int bid=oldSeTable.getBid();
        BankCard bankCard=bankCardMapper.selectByPrimaryKey(bid);
        bankCard.setInitialnum(bankCard.getInitialnum().add(sendMoney));
        //新减
        BigDecimal newSendMoney=updataSeTable.getSpecialexpend();
        bankCard.setInitialnum(bankCard.getInitialnum().subtract(newSendMoney));
        int res1=bankCardMapper.updateByPrimaryKeySelective(bankCard);

        int res = seTableMapper.updateByPrimaryKey(updataSeTable);
        if(res != 1 || res1 != 1){
            return ServerResponse.createByErrorMessage("修改特殊交易失败");
        }
        return ServerResponse.createBySuccessMessage("修改特殊交易成功");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServerResponse deleteSid(int deleteId) {

        SeTable oldSeTable=seTableMapper.selectByPrimaryKey(deleteId);
        //加回
        BigDecimal sendMoney=oldSeTable.getSpecialexpend();
        int bid=oldSeTable.getBid();
        BankCard bankCard=bankCardMapper.selectByPrimaryKey(bid);
        bankCard.setInitialnum(bankCard.getInitialnum().add(sendMoney));
        int res1=bankCardMapper.updateByPrimaryKeySelective(bankCard);

        int res = seTableMapper.deleteByPrimaryKey(deleteId);
        if(res<=0 || res1 != 1){
            return ServerResponse.createByErrorMessage("删除特殊交易失败");
        }
        return ServerResponse.createBySuccessMessage("删除特殊交易成功");
    }
}
